<?php
require_once("./define/define_table.php");

define("ADD_INDIVIDU_FORM", "Formulaire d'ajout d'individu");
define("MODIFY_INDIVIDU_FORM", "Formulaire de modification d'individu");

class Individu {

	protected $id_individu;
	protected $nom;
	protected $prenom;

	public function Individu() {
	}

	public function getNom(){
		return $this->nom;
	}
	
	public function getPrenom(){
	    return $this->prenom;
	}
	
	public function getIdIndividu(){
		return $this->id_individu;
	}
	
	public function setNom($nom){
		$this->nom = $nom;
	}
	
	public function setPrenom($prenom){
		$this->prenom = $prenom;
	}
	
	public function setId($id){
		$this->id_individu = $id;
	}
	
	public function getAllIndividu() {
		global $db;
		try {
			$sql = "SELECT id_individu, nom, prenom FROM " .INDIVIDU_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Individu");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}

	public function getIndividu($id_individu){
		global $db;
		try{
			$sql = "SELECT id_individu, nom, prenom FROM ".INDIVIDU_TABLE." WHERE id_individu = :id_individu";
			$sth = $db->prepare($sql);
			$sth->bindValue(":id_individu", $id_individu, PDO::PARAM_INT);
			$sth->execute();
			return $sth->fetchObject("Individu");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function addOrModify(){
		global $db;
		try{
			if($this->id_individu == null){
				$sql = "INSERT INTO ".INDIVIDU_TABLE." VALUES(null, :nom, :prenom)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":prenom", $this->prenom, PDO::PARAM_STR);
			}
			else{
				$sql = "UPDATE ".INDIVIDU_TABLE." SET nom = :nom, prenom = :prenom WHERE id_individu= :id_individu ";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":prenom", $this->prenom, PDO::PARAM_STR);
				$sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public static function getAddForm(){
		$html = ADD_INDIVIDU_FORM.
				'<form class="ajax_form" id="add_individu_form" action="?module=individu&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._NOM_INDIVIDU_.'</td>' .
				'			<td><input type="text" name="nom" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._PRENOM_INDIVIDU_.'</td>' .
				'			<td><input type="text" name="prenom" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function getModifyForm(){
		$html = MODIFY_INDIVIDU_FORM.
				'<form class="ajax_form" id="modify_individu_form" action="?module=individu&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_INDIVIDU_.'</td>' .
				'			<td><input readonly type="text" name="id_individu" value = "'.$this->id_individu.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NOM_INDIVIDU_.'</td>' .
				'			<td><input type="text" name="nom" value = "'.$this->nom.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._PRENOM_INDIVIDU_.'</td>' .
				'			<td><input type="text" name="prenom" value = "'.$this->prenom.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".INDIVIDU_TABLE." WHERE id_individu = :id_individu";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_individu", $this->id_individu, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}

    public function generateSelect($id = null){
        $array_individu = self::getAllIndividu();
        $html = '<select name="id_individu">';
        foreach($array_individu as $i){
            $selected = ($i->getIdIndividu() == $id) ?  'selected="selected"' : '';
            $html .= '<option value="'.$i->getIdIndividu().'" '.$selected.'>'.$i->getNom().' '.$i->getPrenom().'</option>';
        }
        $html .= '</select>';
        return $html;
    }
}
?>
